package com.campus.help.controller.admin;

import com.campus.help.common.PageResult;
import com.campus.help.common.Result;
import com.campus.help.dto.BroadcastMessageDTO;
import com.campus.help.dto.MessageDTO;
import com.campus.help.service.MessageService;
import com.campus.help.vo.MessageVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * 管理员消息控制器
 */
@RestController
@RequestMapping("/admin/messages")
@RequiredArgsConstructor
public class AdminMessageController {
    
    private final MessageService messageService;
    
    /**
     * 群发消息
     */
    @PostMapping("/broadcast")
    public Result<Void> broadcastMessage(@Valid @RequestBody BroadcastMessageDTO broadcastDTO) {
        // 从请求体中获取发送者信息
        Long adminId = broadcastDTO.getSenderId();
        String adminName = broadcastDTO.getSenderName();
        
        if (adminId == null) {
            adminId = 1L; // 默认管理员ID
        }
        if (adminName == null || adminName.trim().isEmpty()) {
            adminName = "管理员"; // 默认管理员名称
        }
        
        boolean success = messageService.broadcastMessage(broadcastDTO, adminId, adminName);
        return success ? Result.success() : Result.error("群发失败");
    }
    
    /**
     * 发送系统通知
     */
    @PostMapping("/system")
    public Result<Void> sendSystemMessage(
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam(required = false) Long receiverId) {
        
        boolean success = messageService.sendSystemMessage(title, content, receiverId);
        return success ? Result.success() : Result.error("发送失败");
    }
    
    /**
     * 查询所有用户的消息统计
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getMessageStats() {
        try {
            Map<String, Object> stats = messageService.getMessageStats();
            return Result.success(stats);
        } catch (Exception e) {
            return Result.error("获取统计数据失败");
        }
    }
    
    /**
     * 分页查询所有消息（管理员查看）
     */
    @GetMapping
    public Result<PageResult<MessageVO>> getMessages(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        
        try {
            PageResult<MessageVO> result = messageService.getAdminMessages(page, size, type, keyword, startDate, endDate);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("查询消息失败");
        }
    }
    
    /**
     * 分页查询系统消息
     */
    @GetMapping("/system-messages")
    public Result<PageResult<MessageVO>> getSystemMessages(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String type) {
        
        // 查询系统发送的消息
        PageResult<MessageVO> result = messageService.getUserMessages(0L, type, page, size);
        return Result.success(result);
    }
    
    /**
     * 发送单条消息给指定用户
     */
    @PostMapping("/send")
    public Result<Void> sendMessage(
            @Valid @RequestBody MessageDTO messageDTO,
            @RequestParam Long adminId,
            @RequestParam String adminName) {
        
        boolean success = messageService.sendMessage(messageDTO, adminId, adminName);
        return success ? Result.success() : Result.error("发送失败");
    }
    
    /**
     * 删除消息
     */
    @DeleteMapping("/{messageId}")
    public Result<Void> deleteMessage(@PathVariable Long messageId) {
        try {
            boolean success = messageService.deleteMessage(messageId);
            return success ? Result.success() : Result.error("删除失败");
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }
    
    /**
     * 批量删除消息
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteMessages(@RequestBody java.util.List<Long> messageIds) {
        try {
            boolean success = messageService.batchDeleteMessages(messageIds);
            return success ? Result.success() : Result.error("批量删除失败");
        } catch (Exception e) {
            return Result.error("批量删除失败");
        }
    }
}
